
*This file conducts robustness checks of the estimation (Table B.5)

*Bring in regression data
use regression_data.dta, clear

*drop ROW as an originId
drop if originId==62

egen clusterId=group(originId)

************************************************************************************

*Collapsed version: average across importers
collapse logX_theta logL* instrument_* logX_adj logZ_adj clusterId theta se_gyy ln_credit_banks Qc pop real_wage_pwt, by(year sectorId originId)
mkmat se_gyy if originId==1 & year==2010 & sectorId>2, matrix(S)
mkmat theta if originId==1 & year==2010 & sectorId>2, matrix(T)

*****************************************************************************************************************************************
*Robustness Check with different sources of comparative advantage
************************************************************************************************

gen ln_rw=ln(real_wage_pwt)

*FE
local dvars logX_theta
local indvars c.logL#sectorId
local instruments c.instrument_base#sectorId

*Now control for other CA controls
local controls  c.ln_credit_banks#sectorId c.Qc#sectorId
*CES Instrument
ivreghdfe `dvars'  (`indvars'=`instruments') `controls' if sectorId>2 & year==2010, absorb(sectorId originId) 
*Save statistics for table
matrix A=e(V)
matrix b=e(b)

*************************************************************************************
*****Need to compute standard errors corrected for the standard errors of the TEs
*************************************************************************************

preserve
keep if year==2010 & sectorId>2
keep originId sectorId theta logX_theta logL instrument_base ln_credit_banks Qc
sort sectorId

*First step: make matrix of instruments and independent variables, plus the matrix of the derivative of the dependent
*variable with respect to the trade elasticity vector

forvalues x=3/17	{
	gen logX_theta_`x'=-1/theta*logX_theta if sectorId==`x'
	replace logX_theta_`x'=0 if logX_theta_`x'==.
	gen logL_`x'=logL if sectorId==`x'
	replace logL_`x'=0 if logL_`x'==.
	gen ln_credit_banks_`x'=ln_credit_banks if sectorId==`x'
	replace ln_credit_banks_`x'=0 if  ln_credit_banks_`x'==.
	gen Qc_`x'=Qc if sectorId==`x'
	replace Qc_`x'=0 if Qc_`x'==.
	gen instrument_base_`x'=instrument_base if sectorId==`x'
	replace instrument_base_`x'=0 if instrument_base_`x'==.	
}

*Second step: residualize the logX_theta`x' and logL_`x' with respect to the fixed effects

forvalues x=3/17	{	
	reghdfe logX_theta_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logX_thetahat_`x') 
	reghdfe logL_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logLhat_`x') 
		reghdfe ln_credit_banks_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(ln_credit_bankshat_`x') 
	reghdfe Qc_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(Qchat_`x') 
}

reghdfe logX_theta if sectorId>2,  ///
absorb(sectorId originId) res(y) 


*Third step: construct matrices of instruments and residualized independent and dependent variables
mkmat instrument_base_* ln_credit_banks_* Qc_*, matrix(Z)
mkmat logX_thetahat_*, matrix (Y)
mkmat logLhat_* ln_credit_bankshat_* Qchat_*, matrix(L)
mkmat y, matrix(y)

*Fourth Step: Compute the Jacobian matrices for OLS and IV and the reduced form
matrix J = -inv(Z'*L)*Z'*Y

*Compute the variance-covariance matrix and the standard errors

matrix VD = vecdiag(A+J*diag(S)*diag(S)*J')
mata : st_matrix("SE",  st_matrix("VD"):^(.5))

*Preparing for export

matrix M = b\SE
mata: st_matrix("m2", colshape(st_matrix("M")', 90))

matrix M=m2'
matrix list M

putexcel set table3_robust, sheet(SE Estimates) replace
putexcel A1=matrix(M)
restore


*Now All Controls
local controls c.ln_credit_banks#sectorId c.Qc#sectorId c.ln_rw#sectorId
*CES Instrument
ivreghdfe `dvars'  (`indvars'=`instruments') `controls' if sectorId>2 & year==2010, cluster(clusterId) absorb(sectorId originId)

*Save statistics for table
matrix A=e(V)
matrix b=e(b)

*************************************************************************************
*****Need to compute standard errors corrected for the standard errors of the TEs
*************************************************************************************

preserve
keep if year==2010 & sectorId>2
keep originId sectorId theta logX_theta logL instrument_base ln_credit_banks Qc ln_rw
sort sectorId

*First step: make matrix of instruments and independent variables, plus the matrix of the derivative of the dependent
*variable with respect to the trade elasticity vector

forvalues x=3/17	{
	gen logX_theta_`x'=-1/theta*logX_theta if sectorId==`x'
	replace logX_theta_`x'=0 if logX_theta_`x'==.
	gen logL_`x'=logL if sectorId==`x'
	replace logL_`x'=0 if logL_`x'==.
	gen ln_credit_banks_`x'=ln_credit_banks if sectorId==`x'
	replace ln_credit_banks_`x'=0 if  ln_credit_banks_`x'==.
	gen Qc_`x'=Qc if sectorId==`x'
	replace Qc_`x'=0 if Qc_`x'==.
	gen ln_rw_`x'=ln_rw if sectorId==`x'
	replace ln_rw_`x'=0 if ln_rw_`x'==.
	gen instrument_base_`x'=instrument_base if sectorId==`x'
	replace instrument_base_`x'=0 if instrument_base_`x'==.	
}

*Second step: residualize the logX_theta`x' and logL_`x' with respect to the fixed effects

forvalues x=3/17	{	
	reghdfe logX_theta_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logX_thetahat_`x') 
	reghdfe logL_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logLhat_`x') 
		reghdfe ln_credit_banks_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(ln_credit_bankshat_`x') 
	reghdfe Qc_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(Qchat_`x') 
	reghdfe ln_rw_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(ln_rwhat_`x') 
}

reghdfe logX_theta if sectorId>2,  ///
absorb(sectorId originId) res(y) 


*Third step: construct matrices of instruments and residualized independent and dependent variables
mkmat instrument_base_* ln_credit_banks_* Qc_* ln_rw_*, matrix(Z)
mkmat logX_thetahat_*, matrix (Y)
mkmat logLhat_* ln_credit_bankshat_* Qchat_* ln_rwhat_*, matrix(L)
mkmat y, matrix(y)

*Fourth Step: Compute the Jacobian matrices for OLS and IV and the reduced form
matrix J = -inv(Z'*L)*Z'*Y

*Compute the variance-covariance matrix and the standard errors

matrix VD = vecdiag(A+J*diag(S)*diag(S)*J')
mata : st_matrix("SE",  st_matrix("VD"):^(.5))

*Preparing for export

matrix M = b\SE
mata: st_matrix("m2", colshape(st_matrix("M")', 120))

matrix M=m2'
matrix list M

putexcel B1=matrix(M)
restore